const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatHMS = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [hour, minute, second].map(formatNumber).join(':')
}

const formatTimeHM = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  return [hour, minute].map(formatNumber).join(':')
}

const formatTimeMDHM = date => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatTimeYMD = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const compareCellT = (a,b) =>{
  if(b.top){
    if(a.top){
      return b.date.localeCompare(a.date)
    }else{
      return 1
    }
  }else{
    if(a.top){
      return -1
    }else{
      return b.date.localeCompare(a.date)
    }
  }
}

module.exports = {
  formatTime: formatTime,
  formatTimeHM: formatTimeHM,
  formatTimeMDHM: formatTimeMDHM,
  formatTimeYMD: formatTimeYMD,
  formatHMS: formatHMS,
  compareCellT: compareCellT
}
